package com.demo.starter.service.impl;

import com.demo.starter.domain.vo.ArticleCategoryVO;
import com.demo.starter.entity.ArticleCategory;
import com.demo.starter.entity.ArticleCategoryExample;
import com.demo.starter.repository.ArticleCategoryMapper;
import com.demo.starter.service.IArticleCategoryService;
import com.demo.starter.status.ArticleCategoryStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleCategoryServiceImpl implements IArticleCategoryService {

    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    @Override
    public List<ArticleCategoryVO> list() {
        List<ArticleCategoryVO> result = new ArrayList<>();
        final List<ArticleCategory> list = articleCategoryMapper.selectByExample(new ArticleCategoryExample());
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        for (ArticleCategory c :
                list) {
            ArticleCategoryVO vo = new ArticleCategoryVO(c);
            result.add(vo);
        }
        return result;
    }

    @Override
    public ArticleCategoryVO getById(Long categoryId) {
        final ArticleCategory articleCategory = articleCategoryMapper.selectByPrimaryKey(categoryId);
        if (articleCategory != null) {
            return new ArticleCategoryVO(articleCategory);
        }
        return null;
    }

    @Override
    public List<ArticleCategoryVO> listMenuCategories() {
        List<ArticleCategoryVO> result = new ArrayList<>();
        final ArticleCategoryExample example = new ArticleCategoryExample();
        example.createCriteria()
                .andStatusEqualTo(ArticleCategoryStatusEnum.SHOW.getStatus())
                .andIsHotEqualTo(true);
        final List<ArticleCategory> list = articleCategoryMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        for (ArticleCategory c :
                list) {
            ArticleCategoryVO vo = new ArticleCategoryVO(c);
            result.add(vo);
        }
        return result;
    }

}
